package com.netcracker.training.automonic.dao.factories.oracle;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Locale;

import com.netcracker.training.automonic.dao.factories.DAOFactory;
import com.netcracker.training.automonic.dao.roles.ReadWriteDAO;
import com.netcracker.training.automonic.dao.roles.ReadDAO;
import com.netcracker.training.automonic.dao.roles.WriteDAO;
import com.netcracker.training.automonic.dao.roles.oracle.OracleReadDAO;
import com.netcracker.training.automonic.dao.roles.oracle.OracleReadWriteDAO;
import com.netcracker.training.automonic.dao.roles.oracle.OracleWriteDAO;

/**
 * Concrete DAO factory for Oracle Database Express Edition 11g Release 2.
 *
 * @author Vadim Gidulyan
 * 
 * @see DAOFactory
 */

public class OracleDAOFactory extends DAOFactory
{

	// TODO read from file
	/**
	 * Path to Oracle JDBC driver.
	 */
	public static final String DRIVER = "oracle.jdbc.driver.OracleDriver";
	/**
	 * URL of database.
	 */
	public static final String DB_URL = "jdbc:oracle:thin:@localhost:1521:xe";
	
	/**
	 * Connection to Oracle DB.
	 */
	protected Connection connection;
	
	/**
	 * Returns default connection to Oracle DB.
	 * 
	 * @return default connection to Oracle DB otherwise null
	 */
	public Connection createConnection()
	{
		return createConnection("automonic", "amon");
	}
	
	/**
	 * Returns connection to Oracle DB with specified user and password.
	 * 
	 * @param user user
	 * @param password password
	 * 
	 * @return connection to Oracle DB with specified user and password 
	 * otherwise null
	 */
	public Connection createConnection(String user, String password)
	{
		try
		{
			Class.forName(DRIVER);
		}
		catch (ClassNotFoundException e)
		{
			// TODO ClassNotFoundException
			e.printStackTrace();
		}
		
		Locale locale = Locale.getDefault();
		Locale.setDefault(Locale.ENGLISH);
		
		try
		{
			connection = DriverManager.getConnection(DB_URL, user, password);
		}
		catch (SQLException e)
		{
			// TODO SQLException (DriverManager.getConnection())
			e.printStackTrace();
		}
		finally
		{
			Locale.setDefault(locale);
		}
		
		return connection;
	}
	
	/**
	 * Close current connection to Oracle DB.
	 */
	public void closeConnection()
	{
		try
		{
			connection.close();
		}
		catch (SQLException e)
		{
			// TODO SQLException (connection.close())
			e.printStackTrace();
		}
		finally
		{
			connection = null;
		}
	}
	
	/**
	 * Returns current connection to Oracle DB.
	 * 
	 * @return current connection to Oracle DB otherwise default connection
	 * 
	 * @see OracleDAOFactory#createConnection()
	 */
	protected Connection getConnection()
	{
		return connection != null ? connection
		                          : createConnection();
	}
	
	@Override
	public ReadWriteDAO getReadWriteDAO()
	{
		return new OracleReadWriteDAO(getReadDAO(), getWriteDAO());
	}

	@Override
	public ReadDAO getReadDAO()
	{
		return new OracleReadDAO();
	}

	@Override
	public WriteDAO getWriteDAO()
	{
		return new OracleWriteDAO();
	}

}
